﻿package base.form {
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import base.tf.TextFieldUtilsDots;
	import base.dspObj.DoOperations;
	
	public class TfMessage extends TextField {

		private var arrTextDurationMessage: Array;
		public var arrTextIsBadGoodCloseMessage: Array;
		private var colorDurationMessage: uint;
		private var colorBadCloseMessage: uint;
		private var colorGoodCloseMessage: uint;
		private var timerCloseMessage: Timer;
		protected var typeCloseMessage: uint;
		
		public function TfMessage(arrTextDurationMessage: Array, arrTextIsBadGoodCloseMessage: Array, tFormatMessage: TextFormat, colorDurationMessage: int = -1, colorBadCloseMessage: int = -1, colorGoodCloseMessage: int = -1): void {
			this.arrTextDurationMessage = (arrTextDurationMessage != null) ? arrTextDurationMessage : [];
			this.arrTextIsBadGoodCloseMessage = (arrTextIsBadGoodCloseMessage != null) ? arrTextIsBadGoodCloseMessage : [];;
			this.colorDurationMessage = (colorDurationMessage != -1) ? uint(colorDurationMessage) : 0x000000;
			this.colorBadCloseMessage = (colorBadCloseMessage != -1) ? uint(colorBadCloseMessage) : 0xFF0000;
			this.colorGoodCloseMessage = (colorGoodCloseMessage != -1) ? uint(colorGoodCloseMessage) : 0x009900;
			super();
			this.alpha = 0;
			this.visible = false;
			this.width = 150;
			//this.embedFonts = true;
			this.autoSize = TextFieldAutoSize.LEFT;
			this.type = "dynamic";
			this.selectable = false;
			this.multiline = false;
            tFormatMessage = tFormatMessage ? tFormatMessage : new TextFormat(null, 11);
            tFormatMessage.color = this.colorDurationMessage;
            this.setTextFormat(tFormatMessage);
			this.defaultTextFormat = tFormatMessage;
		}
		
		public function showDurationMessage(typeMessage: uint): void {
			TextFieldUtilsDots.stopAddDots(this);
			DoOperations.hideShow(this, 1);
			DoOperations.setColor(this, this.colorDurationMessage, 0)
			this.text = this.arrTextDurationMessage[typeMessage];
			TextFieldUtilsDots.addDots(this);
		}
		
		public function showAndCloseMessage(typeCloseMessage: uint): void {
			if (typeCloseMessage < this.arrTextIsBadGoodCloseMessage.length) {
				TextFieldUtilsDots.stopAddDots(this);
				DoOperations.hideShow(this, 1);
				this.typeCloseMessage = typeCloseMessage;
				DoOperations.setColor(this, [this.colorBadCloseMessage, this.colorGoodCloseMessage][this.arrTextIsBadGoodCloseMessage[typeCloseMessage].isBadGood]);
				this.text = this.arrTextIsBadGoodCloseMessage[typeCloseMessage].text;
				if (this.timerCloseMessage != null) this.timerCloseMessage.stop();
				this.timerCloseMessage = new Timer(Math.round(Math.pow(this.text.length, 0.62) * 3.5 * 60), 1);
				this.timerCloseMessage.addEventListener(TimerEvent.TIMER_COMPLETE, this.onTimerCompleteCloseMessage);
				this.timerCloseMessage.start();
			}
		}

		protected function onTimerCompleteCloseMessage(e: TimerEvent): void {
			DoOperations.hideShow(this, 0);
		}
		
		public function destroy(): void {
			TextFieldUtilsDots.stopAddDots(this);
			DoOperations.hideShow(this, 0);
		}
		
	}
	
}